import Mock from "mockjs"
import Menu from './menu.json'

Mock.mock('/login', 'post', function(options) {
  const user = JSON.parse(options.body);
  let code = 200;
  let message = "成功!";
  let apikey = "";
  if (user.username === "zhangsan" && user.password === "123") {
    apikey = "b9b3a96f7554e3bead2eccf16506c13e";
  } else {
    code = 400;
    message = "失败!";
  }
  return `{"code": ${code},"message": "${message}","result": {"apikey": "${apikey}"}}`
})

Mock.mock('/getMenuList', 'get', function(options) {
  return Menu;
})

Mock.mock('/getItemList', 'post', function(options) {
  //获取查询参数
  const query = JSON.parse(options.body);
  //搜索条件
  let search = query.search;
  //当前页
  let currentPage = query.currentPage;
  //每页显示的条数
  let pageSize = query.pageSize;
  //从数组的哪个下标开始查当前页的数据（偏移量）
  let offset = (currentPage - 1) * pageSize;
  let end = currentPage * pageSize;
  //从localStorage中取出json格式的字符串并转成数组对象
  let items = JSON.parse(localStorage.getItem("items"))
  let total = items.total
  //判断搜索条件
  if (search != '') {
    let res = items.result.filter(val => val.id == query.search)
    items.result = res;
  }
  //分页
  let res2 = items.result.slice(offset, end);
  items.result = res2;
  return items;
})

Mock.mock('/addItem', 'post', function(options) {
  //获取商品数据
  const item = JSON.parse(options.body);
  //商品id
  let id = Math.ceil((Math.random() * 10000));
  item.id = id;
  console.log(item)
  //从localStorage中取出json格式的字符串并转成数组对象
  let items = JSON.parse(localStorage.getItem("items"))
  //修改总数
  items.total += 1;
  //把新添加的商品放到数组中
  items.result.push(item)
  //存储到localStorage中
  localStorage.setItem("items", JSON.stringify(items))
  return `{"code": 200,"message": "成功","result": []}`;
})

Mock.mock('/delItemById', 'post', function(options) {
  //获取商品ID
  const body = JSON.parse(options.body);
  //从localStorage中取出json格式的字符串并转成数组对象
  let items = JSON.parse(localStorage.getItem("items"))
  //删除
  const arr = items.result.filter(val => val.id != body.id)
  items.total -= 1;
  items.result = arr;

  //存储到localStorage中
  localStorage.setItem("items", JSON.stringify(items))
  return `{"code": 200,"message": "成功","result": []}`;
})

Mock.mock('/getItemById', 'get', function(options) {
  //获取商品ID
  const body = JSON.parse(options.body);
  console.log(body)
  //从localStorage中取出json格式的字符串并转成数组对象
  let items = JSON.parse(localStorage.getItem("items"))
  //根据id查找对应的商品
  const item = items.result.find(val => val.id == body.id)
  items.result.length = 0;
  items.result.push(item)
  return items
})

Mock.mock('/editItem', 'post', function(options) {
  //获取商品数据
  const item = JSON.parse(options.body);
  //商品id
  let id = item.id;
  //从localStorage中取出json格式的字符串并转成数组对象
  let items = JSON.parse(localStorage.getItem("items"))

  items.result.forEach(val => {
    if (val.id == id) {
      val.name = item.name
      val.price = item.price
      val.img = item.img
    }
  })
  //存储到localStorage中
  localStorage.setItem("items", JSON.stringify(items))
  return `{"code": 200,"message": "成功","result": []}`;
})
